﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Novel.Sys;

/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/1 10:25:52
*
* */
namespace Novel.Collection.WinForm
{
    public class RegexData
    {
        static Random random = new Random();
        /// <summary>
        /// 获取文章详细
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static Novel.Sys.Model.novel_article CooleArt(string html)
        {
            Novel.Sys.Model.novel_article model_art = new Novel.Sys.Model.novel_article();
            Novel.Sys.BLL.novel_sort bll_sort = new Sys.BLL.novel_sort();
            Novel.Sys.Model.novel_sort model_sort = new Sys.Model.novel_sort();
            try
            {
                Match match = Regex.Match(html, @"<h1>(?<title>.+?)&nbsp;&nbsp;<b(.*)>作者：(?<author>.+?)</b>(.*)</a></span></h1>");
                model_art.Author = match.Groups["author"].Value;//作者
                model_art.Title = match.Groups["title"].Value; //标题
                match = Regex.Match(html, @"<p class=""left"">本书属于<strong>(?<typename>.+?)</strong>(.*)目前<strong>(?<Status>.+?)</strong>(.*)已写了<strong>(?<size>.+?)</strong>(.*)</p>");
                string typename = match.Groups["typename"].Value;
                string Status = match.Groups["Status"].Value;
                model_art.Size = int.Parse(match.Groups["size"].Value.Trim());// 图书大小

                model_sort = bll_sort.GetModel(typename);//分类id
                model_art.Tid = model_sort.ID;
                if (Status.Equals("连载中")) { model_art.Isfull = 0; } //是否连载中
                else { if (Status.Equals("已完成")) { model_art.Isfull = 1; } else { model_art.Isfull = 0; } }




                match = Regex.Match(html, @"<img class=""articleInfoPic"" src=""(?<cover>.+?)""");
                model_art.Cover = match.Groups["cover"].Value;//封面
                match = Regex.Match(html, @"<dd id=""wrap"">([\S\s]*?)</dd>");
                model_art.Intor = match.Groups[1].Value;//简介

                model_art.Hits = 0;
                model_art.Permission = 0;//授权级别 0未授权
                model_art.Created = DateTime.Now;//创建时间
                model_art.Display = 0;//是否审核  0未审核
                model_art.First = 0;//是否本站收发 0 不是
                model_art.Flag = "";

                match = Regex.Match(html, @"<p class=""right""><a href=""(?<infoUrl>.+?)""");
                model_art.InfoUrl = match.Groups["infoUrl"].Value;




                return model_art;
            }
            catch { return null; }
        }



        public static Dictionary<string, string> CooleChapter(string html)
        {
            Dictionary<string, string> list = new Dictionary<string, string>();
            string regex = @"<td class=""ccss"">\s*<a href=""(?<chapterID>.+?).html"">(?<chapterName>.+?)</a>\s*</td>";
            foreach (Match m in Regex.Matches(html, regex))
            {
                list.Add(m.Groups["chapterID"].Value, m.Groups["chapterName"].Value);
            }
            return list;
        }

        /// <summary>
        /// 获取文章详细
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static Novel.Sys.Model.novel_article CooleBook(string html)
        {
            Novel.Sys.Model.novel_article model_art = new Novel.Sys.Model.novel_article();
            Novel.Sys.BLL.novel_sort bll_sort = new Sys.BLL.novel_sort();
            Novel.Sys.Model.novel_sort model_sort = new Sys.Model.novel_sort();
            try
            {
                Match match = Regex.Match(html, @"<h1>(?<title>.+?)全文阅读</h1>");
               // model_art.Author = match.Groups["author"].Value;//作者
                model_art.Title = match.Groups["title"].Value; //标题
                match = Regex.Match(html, @"小说类别</th><td>&nbsp;(?<sort>.+?)</td>");
                string typename = match.Groups["sort"].Value;

                match = Regex.Match(html, @"小说状态</th><td>&nbsp;(?<status>.+?)</td>");
                string Status = match.Groups["status"].Value;

                match = Regex.Match(html, @"小说作者</th><td>&nbsp;(?<Author>.+?)</td>");
                model_art.Author = match.Groups["Author"].Value;

                match = Regex.Match(html, @"全文长度</th><td>&nbsp;(?<size>.+?)字</td>");
                string Size = match.Groups["size"].Value;

                try { model_art.Size = int.Parse(Size); }
                catch { model_art.Size = 0; }

                model_sort = bll_sort.GetModel(SortNameTo(typename));//分类id
                model_art.Tid = model_sort.ID;
                if (Status.Equals("连载中")) { model_art.Isfull = 0; } //是否连载中
                else { if (Status.Equals("已完成")) { model_art.Isfull = 1; } else { model_art.Isfull = 0; } }




                match = Regex.Match(html, @"<img class=""articleInfoPic"" src=""(?<cover>.+?)""");
                model_art.Cover = match.Groups["cover"].Value;//封面
                match = Regex.Match(html, @"<dd id=""wrap"">([\S\s]*?)</dd>");
                model_art.Intor = match.Groups[1].Value;//简介

                model_art.Hits = 0;
                model_art.Permission = 0;//授权级别 0未授权
                model_art.Created = DateTime.Now;//创建时间
                model_art.Display = 0;//是否审核  0未审核
                model_art.First = 0;//是否本站收发 0 不是
                model_art.Flag = "";

                match = Regex.Match(html, @"<p class=""right""><a href=""(?<infoUrl>.+?)""");
                model_art.InfoUrl = match.Groups["infoUrl"].Value;




                return model_art;
            }
            catch { return null; }
        }


        private static string SortNameTo(string SortName)
        {
            switch (SortName.Trim())
            {
                case "科幻灵异":
                    return "科幻小说";
                case "网游竞技":
                    return "网游动漫";
                default:
                    return SortName.Trim();
            }
        }
    
    
    
    
    
    
    
    
    
    
    }
}
